/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.five9.util;

import java.util.logging.*;

/**
 *
 * @author nromero
 */
public class ConsoleLoggingHandler extends Handler {
    //private {View} window = null; //the window to which the logging is done

    private Formatter formatter = null;
    private Level level = null;
    private static ConsoleLoggingHandler handler = null; //the singleton instance

    private ConsoleLoggingHandler() {
        configure();
    }

    @Override
    public void publish(LogRecord record) {
        String message = null;
        //check if the record is loggable
        if (!isLoggable(record)) {
            return;
        }
        try {
            message = getFormatter().format(record);
        } catch (Exception e) {
            reportError(null, e, ErrorManager.FORMAT_FAILURE);
        }

        try {
            System.out.print(message + System.getProperty("line.separator"));
        } catch (Exception ex) {
            reportError(null, ex, ErrorManager.WRITE_FAILURE);
        }
        System.out.println(record);
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void flush() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void close() throws SecurityException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public static synchronized ConsoleLoggingHandler getInstance() {

        if (handler == null) {
            handler = new ConsoleLoggingHandler();
        }
        return handler;
    }

    private void configure() {
        LogManager _manager = LogManager.getLogManager();
        String _className = this.getClass().getName();
        String _level = _manager.getProperty(_className + ".level");
        String _filter = _manager.getProperty(_className + ".filter");
        String _formatter = _manager.getProperty(_className + ".formatter");

        //accessing super class methods to set the parameters
        setLevel(_level != null ? Level.parse(_level) : Level.INFO);
        setFilter(makeFilter(_filter));
        setFormatter(makeFormatter(_formatter));
    }

    private Filter makeFilter(String filterName) {
        Class c = null;
        Filter f = null;
        try {
            c = Class.forName(filterName);
            f = (Filter) c.newInstance();
        } catch (Exception e) {
            System.out.println("There was a problem to load the filter class: "
                    + filterName);
        }
        return f;
    }

    private Formatter makeFormatter(String formatterName) {
        Class c = null;
        Formatter f = null;

        try {
            c = Class.forName(formatterName);
            f = (Formatter) c.newInstance();
        } catch (Exception e) {
            //f = new SimpleFormatter();
            f = new BriefLogFormatter();
        }
        return f;
    }
}
